<template>
  <div>
    <!-- 数据列表 -->
    <el-table
      :data="menulist"
      border
      style="width: 100%"
      row-key="id"
      :tree-props="{ children: 'children' }"
      default-expand-all
    >
      <el-table-column prop="id" label="菜单编号" width="180">
      </el-table-column>
      <el-table-column prop="title" label="菜单名称" width="180">
      </el-table-column>
      <el-table-column prop="pid" label="上级菜单"> </el-table-column>
      <el-table-column prop="icon" label="菜单图标" width="180">
        <template slot-scope="item">
          <div>
            <i :class="item.row.icon"></i>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="菜单地址" width="180">
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="item">
          <div>
            <span v-if="item.row.status == 1">启用</span>
            <span v-else>禁用</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="操作">
        <template slot-scope="item">
          <div>
            <el-button
              circle
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="edit(item.row.id)"
            ></el-button>
            <el-button
              circle
              type="danger"
              icon="el-icon-delete
"
              @click="del(item.row.id)"
              size="mini"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
//调用辅助性函数
import { mapGetters, mapActions } from "vuex";

import { menuDelete } from "../../request/api";

export default {
  computed: {
    ...mapGetters({
      menulist: "menu/getMenu",
    }),
  },
  methods: {
    //封装一个编辑事件
    edit(id) {
      this.$emit("edit", id);
    },
    ...mapActions({
      getMenu: "menu/getMenuAction",
    }),
    del(id) {
      this.$confirm("确定要删除这条数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          menuDelete({ id }).then((res) => {
            console.log(res, "删除菜单");
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.getMenu();
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    //组件一加载就调用列表接口
    this.getMenu();
  },
};
</script>

<style scoped></style>
